package a51qindian.com.comprehensive.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.li.widget.DoubleTextView;
import com.li.widget.MultipleLayout;
import com.li.widget.TopNavigationBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import a51qindian.com.comprehensive.Const;
import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.adapter.CrashDetailAdapter;
import a51qindian.com.comprehensive.base.QDBaseActivity;
import a51qindian.com.comprehensive.items.LinearItemDecoration;
import a51qindian.com.comprehensive.model.IncomeBaseModel;
import a51qindian.com.comprehensive.model.Withdraw;
import a51qindian.com.comprehensive.net.ObservableHelper;
import a51qindian.com.comprehensive.net.RetrofitCreator;
import a51qindian.com.comprehensive.net.SelectorObserver;
import butterknife.BindView;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/9/20 0020
 * Description: 提现记录
 */

public class GetCrashDetailActivity extends QDBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.recycler_crash_detail)
    RecyclerView mRecyclerCrashDetail;
    @BindView(R.id.layout_content)
    MultipleLayout mLayoutContent;
    @BindView(R.id.dtv_num)
    DoubleTextView mDtvNum;
    @BindView(R.id.dtv_total)
    DoubleTextView mDtvTotal;
    @BindView(R.id.layout_refresh)
    SmartRefreshLayout mLayoutRefresh;
    private CrashDetailAdapter mCrashDetailAdapter;
    private List<Withdraw> mWithdrawList;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_get_crash_detail;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mWithdrawList=new ArrayList<>();
        mTopNavigation.setNavigationBarListener(this);
        mRecyclerCrashDetail.setLayoutManager(new LinearLayoutManager(this));
        mCrashDetailAdapter = new CrashDetailAdapter(this);
        mRecyclerCrashDetail.setAdapter(mCrashDetailAdapter);
        mRecyclerCrashDetail.addItemDecoration(new LinearItemDecoration());
        setSmartRefreshLayout(mLayoutRefresh);
        getWithdrawLog(mUid,"0");
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        super.onRefresh(refreshlayout);
        getWithdrawLog(mUid,"0");
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        super.onLoadmore(refreshlayout);
        getWithdrawLog(mUid,String.valueOf(mPage+1));
    }

    /**
     * 获取提现记录
     * @param uid   用户编号
     */
    private void getWithdrawLog(String uid,String page){
        Map<String,String> map=new HashMap<>();
        map.put("uid",uid);
        map.put("page",page);
        mLayoutContent.onLoadingWithContent("加载中");
        ObservableHelper.createObservable(RetrofitCreator.createService().withdrawLog(map))
                .subscribe(new SelectorObserver<IncomeBaseModel<List<Withdraw>>>() {
                    @Override
                    protected void onErrorCodeInfo(int code, String info) {
                        mLayoutContent.onSuccess();
                        stopRefreshLoad();
                        mLayoutRefresh.setLoadmoreFinished(true);
                        showToast(info);
                    }

                    @Override
                    protected void onSuccess(IncomeBaseModel<List<Withdraw>> listBaseModel) {
                        List<Withdraw> withdrawList = listBaseModel.getData();
                        mLayoutContent.onSuccess();
                        if (mIsRefresh) {
                            mPage=0;
                            mWithdrawList.clear();
                            mLayoutRefresh.setLoadmoreFinished(false);
                        }else {
                            mPage++;
                        }
                        stopRefreshLoad();
                        if (!mIsRefresh&&withdrawList.size()< Const.DEFAULT_PAGE_SIZE) {
                            mLayoutRefresh.setLoadmoreFinished(true);
                        }
                        mWithdrawList.addAll(withdrawList);
                        mCrashDetailAdapter.setWithdrawList(mWithdrawList);
                        mDtvNum.setTxtRight(listBaseModel.getCount().getCount());
                        mDtvTotal.setTxtRight(listBaseModel.getCount().getTotal_money());
                    }

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        showToast(e.getMessage());
                        mLayoutContent.onSuccess();
                        stopRefreshLoad();
                    }

                });
    }
}
